#include "EpilReturnStatementParser.h"
#include "EpilExpressionListParser.h"
#include "EpilParserUtilities.h"
#include "EpilPointerHelper.h"


EpilReturnStatementParser::EpilReturnStatementParser( EpilTokenStream & tokens )
: EpilSyntaxParserBase( tokens )
{
}


void EpilReturnStatementParser::Parse()
{
	// stat -> RETURN explist
	m_statement = new EpilReturnStatementCode( this->GetTokens().GetCurrentToken().location );

	this->GetTokens().MoveToNext();

	if( EpilParserUtilities::IsBlockFollow( this->GetTokens() ) ||
		this->GetTokens().GetCurrentToken().type == ETT_OpSemicolon )
	{
		return;
	}

	EpilExpressionListParser exprListParser( this->GetTokens() );
	try
	{
		exprListParser.Parse();
	}
	catch( ... )
	{
		EpilPointerHelper::SafeDelete( m_statement );
		throw;
	}
	m_statement->SetExpressions( exprListParser.GetExpressions() );
}